package com.wang.tank;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.util.Vector;

/**
 * @author 王先锋
 * @version 1.0
 * 绘图区
 */
//为了监听，实现keyListener
public class MyPanel extends JPanel implements KeyListener, Runnable {
    //定义我的坦克
    Hero hero = null;
    //定义敌人坦克放入到vector集合中
    Vector<EnemTank> enemytanks = new Vector<>();
    //定义Vector，用于存放炸弹
    Vector<Bomb> bombs = new Vector<>();
    //定义一个存放Node对象的vector，用于恢复敌人坦克的信息
    Vector<Node> nodes = new Vector<>();
    int enemyTankSize = 4;

    //定义三张照片
    Image image1 = null;
    Image image2 = null;
    Image image3 = null;


    public MyPanel(String key) {
        //先判断记录文件是否存在
        File file = new File(Recorder.getRecordFile());
        if (file.exists()) {
            nodes = Recorder.getNodesAndEnemyTankRec();
        } else {
            System.out.println("文件不存在");
            key = "1";
        }

        //将mypanel对象的enemyTanks设置Recorder的enemyTanks
        Recorder.setEnemyTanks(enemytanks);

        hero = new Hero(500, 100);//初始化坦克
         hero.setSpeed(6);

        switch (key) {
            case "1":
                //初始化敌人的坦克
                for (int i = 0; i < enemyTankSize; i++) {
                    EnemTank enemyTank = new EnemTank(100 * i + 1, 0);
                    //将enemyTanks设置给enemyTank
                    enemyTank.setEnemyTanks(enemytanks);
                    enemyTank.setDirect(2);
                    //启动敌人坦克线程，让他动起来
                    new Thread(enemyTank).start();
                    Shot shot = new Shot(enemyTank.getX() + 20, enemyTank.getY() + 60, enemyTank.getDirect());
                    //加入enemyTank的vector成员
                    enemyTank.shots.add(shot);
                    new Thread(shot).start();
                    enemytanks.add(enemyTank);
                }
                break;
            case "2":
                //初始化敌人的坦克
                for (int i = 0; i < nodes.size(); i++) {
                    Node node = nodes.get(i);
                    EnemTank enemyTank = new EnemTank(node.getX(), node.getY());
                    //将enemyTanks设置给enemyTank
                    enemyTank.setEnemyTanks(enemytanks);
                    enemyTank.setDirect(node.getDirect());
                    //启动敌人坦克线程，让他动起来
                    new Thread(enemyTank).start();
                    Shot shot = new Shot(enemyTank.getX() + 20, enemyTank.getY() + 60, enemyTank.getDirect());
                    //加入enemyTank的vector成员
                    enemyTank.shots.add(shot);
                    new Thread(shot).start();
                    enemytanks.add(enemyTank);
                }
                break;
            default:
                System.out.println("你的输入有误");
        }

        image1 = Toolkit.getDefaultToolkit().createImage(Panel.class.getResource("/bomb_1.png"));
        image2 = Toolkit.getDefaultToolkit().createImage(Panel.class.getResource("/bomb_2.png"));
        image3 = Toolkit.getDefaultToolkit().createImage(Panel.class.getResource("/bomb_3.png"));
        //播放指定音频
        new AePlayWave("src\\10013.wav").start();
    }

    //编写方法，显示我方击败坦克
    public void showInfo(Graphics g) {
        //画出玩家总成绩
        g.setColor(Color.BLACK);
        Font font = new Font("宋体", Font.BOLD, 25);
        g.setFont(font);

        g.drawString("累积击毁坦克", 1020, 30);
        drawTank(1020, 60, g, 0, 0);
        g.setColor(Color.BLACK);
        g.drawString(Recorder.getAllEnemyTankNum() + "", 1080, 100);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.fillRect(0, 0, 1000, 750);//填充矩形
        showInfo(g);
        if (hero != null && hero.isLive) {
            //画出坦克封装方法
            drawTank(hero.getX(), hero.getY(), g, hero.getDirect(), 1);
            //画出hero子弹
        }
        for (int i = 0; i < hero.shots.size(); i++) {
            Shot shot = hero.shots.get(i);
            if (shot != null && shot.isLive != false) {
                g.draw3DRect(shot.getX(), shot.getY(), 1, 1, false);
            } else {//如果shot对象失效
                hero.shots.remove(shot);
            }
        }


        //如果bobms集合中有对象就显示
        for (int i = 0; i < bombs.size(); i++) {
            //取出炸弹
            Bomb bomb = bombs.get(i);
            //根据当前这个bobm对象的life值画出对应图片
            if (bomb.life > 6) {
                g.drawImage(image1, bomb.x, bomb.y, 60, 60, this);
            } else if (bomb.life > 3) {
                g.drawImage(image2, bomb.x, bomb.y, 60, 60, this);
            } else {
                g.drawImage(image3, bomb.x, bomb.y, 60, 60, this);
            }
            //让炸弹生命值减小
            bomb.lifeDown();
            ;
            //如果bomb的Life为0，就从bombs的集合中删除
            if (bomb.life == 0) {
                bombs.remove(bomb);
            }
        }

        //绘画出敌人坦克
        for (int i = 0; i < enemytanks.size(); i++) {
            //  取出坦克
            EnemTank enemTank = enemytanks.get(i);
            //判断当前坦克是否存活
            if (enemTank.isLive) {
                drawTank(enemTank.getX(), enemTank.getY(), g, enemTank.getDirect(), 0);
                //取出子弹
                for (int j = 0; j < enemTank.shots.size(); j++) {
                    //取出子弹
                    Shot shot = enemTank.shots.get(j);
                    //绘制
                    if (shot.isLive) {
                        g.draw3DRect(shot.getX(), shot.getY(), 2, 2, false);
                    } else {
                        //  取出子弹
                        enemTank.shots.remove(shot);
                    }
                }
            }
        }
    }
    //编写方法画出坦克

    /**
     * @param x      坦克横坐标
     * @param y      坦克纵坐标
     * @param g      画笔
     * @param direct 坦克方向
     * @param type   坦克类型
     */
    public void drawTank(int x, int y, Graphics g, int direct, int type) {
        switch (type) {//根据不同类型坦克，设置不同颜色
            case 0:
                g.setColor(Color.cyan);
                break;
            case 1:
                g.setColor(Color.yellow);
                break;
        }
        //根据坦克方向，来绘制坦克
        switch (direct) {
            case 0:
                g.fill3DRect(x, y, 10, 60, false);//画出左边的轮子
                g.fill3DRect(x + 30, y, 10, 60, false);//画出右边的轮子
                g.fill3DRect(x + 10, y + 10, 20, 40, false);//画出坦克的盖子
                g.fillOval(x + 10, y + 20, 20, 20);//画出圆盖子
                g.drawLine(x + 20, y + 30, x + 20, y);
                break;
            case 1:
                g.fill3DRect(x, y, 60, 10, false);//画出左边的轮子
                g.fill3DRect(x, y + 30, 60, 10, false);//画出右边的轮子
                g.fill3DRect(x + 10, y + 10, 40, 20, false);//画出坦克的盖子
                g.fillOval(x + 20, y + 10, 20, 20);//画出圆盖子
                g.drawLine(x + 30, y + 20, x + 60, y + 20);
                break;
            case 2:
                g.fill3DRect(x, y, 10, 60, false);//画出左边的轮子
                g.fill3DRect(x + 30, y, 10, 60, false);//画出右边的轮子
                g.fill3DRect(x + 10, y + 10, 20, 40, false);//画出坦克的盖子
                g.fillOval(x + 10, y + 20, 20, 20);//画出圆盖子
                g.drawLine(x + 20, y + 30, x + 20, y + 60);
                break;
            case 3:
                g.fill3DRect(x, y, 60, 10, false);//画出左边的轮子
                g.fill3DRect(x, y + 30, 60, 10, false);//画出右边的轮子
                g.fill3DRect(x + 10, y + 10, 40, 20, false);//画出坦克的盖子
                g.fillOval(x + 20, y + 10, 20, 20);//画出圆盖子
                g.drawLine(x + 30, y + 20, x, y + 20);
                break;
            default:
                System.out.println("暂时没有处理");
        }
    }

    //如果我们的坦克可以发射多个子弹
    //在判断我方子弹是否击中敌人坦克时，就需要我们的子弹集合
    //所有的子弹，都取出和敌人的所有坦克，进行判断
    public void hitEnemyTank() {
        //遍历我们的子弹
        for (int i = 0; i < hero.shots.size(); i++) {
            Shot shot = hero.shots.get(i);

            //判断是否击中敌人坦克
            if (shot != null && shot.isLive) {//当我的子弹存活时
                //遍历敌人所有坦克
                for (int j = 0; j < enemytanks.size(); j++) {
                    EnemTank enemyTank = enemytanks.get(j);
                    hitTank(shot, enemyTank);
                }

            }
        }
    }

    public void hitTank(Shot s, Tank enemyTank) {
        //判断子弹是否击中坦克
        switch (enemyTank.getDirect()) {
            case 0:
            case 2:
                if (s.getX() > enemyTank.getX() && s.getX() < enemyTank.getX() + 40
                        && s.getY() > enemyTank.getY() && s.getY() < enemyTank.getY() + 60) {
                    s.isLive = false;
                    enemyTank.isLive = false;
                    //创建Bomb对象，加入到bobms集合
                    //当我的子弹击中敌人的坦克后，将enemytank从Vector中拿掉
                    enemytanks.remove(enemyTank);
                    //当我方击毁敌方坦克后，将数据allEnemyTankNum加加
                    if (enemyTank instanceof EnemTank) {
                        Recorder.addallEnemyTankNum();
                    }
                    Bomb bomb = new Bomb(enemyTank.getX(), enemyTank.getY());
                    bombs.add(bomb);
                }
                break;
            case 1:
            case 3:
                if (s.getX() > enemyTank.getX() && s.getX() < enemyTank.getX() + 60 &&
                        s.getY() > enemyTank.getY() && s.getY() < enemyTank.getY() + 40) {
                    s.isLive = false;
                    enemyTank.isLive = false;
                    //创建Bomb对象，加入到bobms集合
                    enemytanks.remove(enemyTank);
                    //当我方击毁敌方坦克后，将数据allEnemyTankNum加加
                    if (enemyTank instanceof EnemTank) {
                        Recorder.addallEnemyTankNum();
                    }
                    Bomb bomb = new Bomb(enemyTank.getX(), enemyTank.getY());
                    bombs.add(bomb);
                }
                break;
        }
    }

    //编写方法，判断敌人坦克是否击中我的坦克
    public void hitHero() {
        for (int i = 0; i < enemytanks.size(); i++) {
            //取出敌人坦克
            EnemTank enemyTank = enemytanks.get(i);
            //遍历enemyTa对象的所有子弹
            for (int j = 0; j < enemyTank.shots.size(); j++) {
                //取出子弹
                Shot shot = enemyTank.shots.get(j);
                //判断shot还否击中我的坦克
                if (hero.isLive && shot.isLive) {
                    hitTank(shot, hero);
                }
            }
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_W) {
            //改变坦克方向
            hero.setDirect(0);
            if (hero.getY() > 0)
                hero.moveUp();
        } else if (e.getKeyCode() == KeyEvent.VK_D) {
            //改变坦克方向
            hero.setDirect(1);
            if (hero.getX() + 60 < 1000)
                hero.moveRight();
        } else if (e.getKeyCode() == KeyEvent.VK_S) {
            //改变坦克方向
            hero.setDirect(2);
            if (hero.getY() + 60 < 750)
                hero.moveDown();
        } else if (e.getKeyCode() == KeyEvent.VK_A) {
            //改变坦克方向
            hero.setDirect(3);
            if (hero.getX() > 0)
                hero.moveLeft();
        }
        //如果用户按下j，就发射
        if (e.getKeyCode() == KeyEvent.VK_J) {
            //判断hero的子弹是否销毁
            //if (hero.shot == null && !hero.shot.isLive) {
            //    hero.shotEnemyTank();
            //}
            hero.shotEnemyTank();
        }
        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent keyEvent) {

    }

    @Override
    public void run() {//每个100毫秒刷新绘图区域
        while (true) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            hitEnemyTank();//我们是否击中敌人
            hitHero();//敌人是否击中我们

            this.repaint();
        }
    }
}
